package com.igame.Service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.igame.Service.CommonService;
import com.igame.Service.InformationService;
import com.igame.common.PageResult;
import com.igame.common.Result;
import com.igame.dto.InformationPageDto;
import com.igame.entity.Information;
import com.igame.mapper.InformationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class InformationImpl implements InformationService {


    @Autowired
    InformationMapper informationMapper;

    @Autowired
    CommonService commonService;

    @Override
    @Transactional
    public Result addInformation(@RequestParam("title") String title,
                                 @RequestParam("content") String content,
                                 @RequestParam("userId") Integer userId,
                                 @RequestPart(value = "image", required = false) MultipartFile image) {

        try {
            Information information = new Information();
            String newTitle = title.replace("\"", "");
            String newContent = content.replace("\"", "");

            if (newTitle.equals("") || newContent.equals("")){
                throw new RuntimeException("标题或内容不能为空!");
            }else {
                information.setTitle(title);
                information.setContent(content);
            }
            if (image!=null){
                String upload = commonService.upload(image);
                information.setImage(upload);
            }else {
                throw new RuntimeException("请添加一张图片作为封面");
            }
            information.setId(null);
            information.setCreateUser(userId);
            information.setCreateTime(LocalDateTime.now());
            int insert = informationMapper.insert(information);
            if (insert <= 0) {
                throw new RuntimeException("添加资讯出现错误，请稍后再试");
            }
            return Result.success();
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error(e.getMessage());
        }
    }

    @Override
    public PageResult pageQuery(InformationPageDto informationPageDto) {

        PageHelper.startPage(informationPageDto.getPage(),informationPageDto.getPageSize());

        Page<Information> page = informationMapper.pageQuery(informationPageDto);

        // 使用 page 获取 total 和 records 并且将这两个数据封装给PageResult
        long total = page.getTotal();
        List<Information> records = page.getResult();


        return new PageResult(total,records);

    }

    @Override
    public Result<Information> getInformationById(Integer id) {
        Information information = informationMapper.selectInformationById(id);

        return Result.success(information);
    }
}

